import React from 'react';
import { Route, Switch, Redirect } from 'react-router-dom';

const RouterView = props => {
  const routerCom = props.routes.filter(v => v.component);
  const redirCom = props.routes.filter(v => v.redirect);
  return (
    <Switch>
      {
        routerCom.map((item, index) => {
          return (
            <Route key={index} path={item.path} render={props => {
              return (
                <item.component {...props} routes={item.children} />
              );
            }}
            ></Route>
          );
        })
      }
      {
        redirCom.map((item, index) => {
          return <Redirect key={index} from='/' to={{
            pathname: item.redirect,
            params: {
              query: ''
            }
          }}></Redirect>
        })
      }
    </Switch>
  );
};

export default RouterView;
